package cn.topevery.garden.wapi.controller;

import cn.topevery.garden.dal.logic.CleaningLogic;
import cn.topevery.garden.dal.util.FileUtils;
import cn.topevery.garden.entity.dto.CleaningDTO;
import cn.topevery.garden.entity.po.Cleaning;
import cn.topevery.garden.entity.po.GreFile;
import cn.topevery.garden.entity.query.CleaningQuery;
import cn.topevery.garden.entity.vo.CleaningVO;
import com.github.dozermapper.core.Mapper;
import hw.topevery.framework.db.entity.DataPager;
import hw.topevery.framework.util.ExcelUtil;
import hw.topevery.framework.web.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * 清扫保洁管理
 *
 * @author code-generate
 * @date 2022-11-25
 */
@Api(tags = "清扫保洁管理")
@RestController
@RequestMapping("/cleaning")
public class CleaningController extends BaseController {

    @Autowired
    private Mapper mapper;

    @Autowired
    CleaningLogic cleaningLogic;

    @Autowired
    FileUtils fileUtils;


    /**
     * 获取清扫保洁管理分页列表
     *
     * @param query
     * @return
     */
    @ApiOperation(value = "获取清扫保洁管理页列表")
    @PostMapping("/listPage")
    public JsonResult<DataPager<CleaningVO>> listPage(@RequestBody CleaningQuery query) {
        DataPager<CleaningVO> page = cleaningLogic.listPage(query);
        page.records.forEach(vo -> vo.setFileList(fileUtils.getFile(vo.getId(), GreFile.TYPE_CLEANING)));
        return JsonResult.ok(page);
    }

    @ApiOperation("新增/编辑 清扫保洁管理信息")
    @PostMapping("/saveOrUpdate")
    public JsonResult<Boolean> saveOrUpdate(@Valid @RequestBody CleaningDTO dto) {
        Cleaning cleaning = mapper.map(dto, Cleaning.class);
        String userId = getUser().id.toString();
        cleaningLogic.saveOrUpdate(userId, cleaning);
        fileUtils.saveFile(dto.getFileList(), cleaning.getId(), userId, GreFile.TYPE_CLEANING);
        return JsonResult.ok(true);
    }

    @ApiOperation("通过Id获取详情")
    @GetMapping("/getInfoById")
    public JsonResult<CleaningVO> getInfoById(@RequestParam Integer id) {
        Cleaning entity = cleaningLogic.getEntity(id);
        CleaningVO vo = mapper.map(entity, CleaningVO.class);
        vo.setFileList(fileUtils.getFile(vo.getId(), GreFile.TYPE_CLEANING));
        return JsonResult.ok(vo);
    }

    @ApiOperation("根据自增ID批量删除")
    @PostMapping("/deleteBatchById")
    public JsonResult<Boolean> deleteBatchById(@RequestBody Integer[] ids) {
        cleaningLogic.deleteBatchById(getUser().id.toString(), ids);
        return JsonResult.ok(true);
    }

    @ApiOperation("导出清扫保洁管理信息")
    @GetMapping("/export")
    public void export(CleaningQuery query, HttpServletResponse response) {
        List<CleaningVO> list = cleaningLogic.listPage(query).records;
        ExcelUtil.exportExcel(list, "清扫保洁管理", "sheet1", CleaningVO.class, "清扫保洁管理", response);
    }

}
